<html>
<head><meta charset="utf-8"><title>Interacting with non-repr(C) types in FFI · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html">Interacting with non-repr(C) types in FFI</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="207360864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207360864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207360864">(Aug 19 2020 at 01:08)</a>:</h4>
<p>Is there any way to expose pointers to rust data in FFI without converting my whole project to use <code>repr(C)</code> (and lose ADT magic) or making copies at the FFI boundary? How is this usually handled in big projects?</p>



<a name="207360900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207360900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207360900">(Aug 19 2020 at 01:09)</a>:</h4>
<p>I guess this is an unsafe-code-guidelines question although it's pretty basic</p>



<a name="207361004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361004">(Aug 19 2020 at 01:11)</a>:</h4>
<p>You have to use <code>repr(C)</code>, yes</p>



<a name="207361049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361049">(Aug 19 2020 at 01:12)</a>:</h4>
<p>Note that you don't have to <code>repr(C)</code> <em>everything</em>, just the stuff that's exposed to FFI.</p>



<a name="207361052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361052">(Aug 19 2020 at 01:12)</a>:</h4>
<p>so do you have to roll your own ADTs then? Is there any <code>repr(C)</code> like standard for nontrivial enums</p>



<a name="207361132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361132">(Aug 19 2020 at 01:14)</a>:</h4>
<p>alternatively (and I'm sure this doesn't exist) it would be great to be able to assert that my type has a particular layout, with the understanding that rust can change the rules and I just want to know if they change rather than get UB</p>



<a name="207361154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361154">(Aug 19 2020 at 01:15)</a>:</h4>
<p>Are they <em>opaque</em> pointers? I think that's fine.</p>



<a name="207361158"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361158" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361158">(Aug 19 2020 at 01:15)</a>:</h4>
<p>so you mean like accessor methods for everything?</p>



<a name="207361160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361160">(Aug 19 2020 at 01:15)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> You'd need to make your own tagged-unions, yes.</p>



<a name="207361210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361210">(Aug 19 2020 at 01:16)</a>:</h4>
<p>There should be a crate for that :P</p>



<a name="207361215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361215">(Aug 19 2020 at 01:16)</a>:</h4>
<p>if the other side doesn't directly touch the pointer, then its layout doesn't matter</p>



<a name="207361255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361255">(Aug 19 2020 at 01:17)</a>:</h4>
<p>You could do that with accessors, then cross-lang LTO to inline it all</p>



<a name="207361261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361261">(Aug 19 2020 at 01:17)</a>:</h4>
<p>I'm thinking of the use case where I have a huge graph data structure, like an AST, and I don't really want to give up on ADTs but I also don't want to copy, and navigate the data structure from foreign code</p>



<a name="207361317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361317">(Aug 19 2020 at 01:18)</a>:</h4>
<p>Accessors would work for that although they would be a performance hit unless you could somehow inline them across languages</p>



<a name="207361336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361336">(Aug 19 2020 at 01:19)</a>:</h4>
<p>ah, you said that</p>



<a name="207361341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361341">(Aug 19 2020 at 01:19)</a>:</h4>
<p>I see, LTO can do this kind of thing</p>



<a name="207361444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361444">(Aug 19 2020 at 01:21)</a>:</h4>
<p>Where's the RFC I can vote for to standardize a repr(C) tagged union</p>



<a name="207361488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361488">(Aug 19 2020 at 01:22)</a>:</h4>
<p>"be the change you want to see in the world"</p>



<a name="207361499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207361499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207361499">(Aug 19 2020 at 01:22)</a>:</h4>
<p>Specifically, open an MCP here: <a href="https://github.com/rust-lang/lang-team/issues">https://github.com/rust-lang/lang-team/issues</a></p>



<a name="207364104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207364104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207364104">(Aug 19 2020 at 02:26)</a>:</h4>
<p><a href="https://github.com/rust-lang/rfcs/pull/2195">https://github.com/rust-lang/rfcs/pull/2195</a> (It's already implemented and stable)</p>



<a name="207364222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207364222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207364222">(Aug 19 2020 at 02:28)</a>:</h4>
<p>Yeah, while setting up the MCP I tried doing some tests to see what current rust does and was surprised to find that repr(C) is completely working</p>



<a name="207364318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207364318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207364318">(Aug 19 2020 at 02:30)</a>:</h4>
<p>I'd point to the reference, but I tried adding it, got a lot of feedback my approach to documentation for it was in the wrong direction and never mustered the energy to do a second take. In the mean time, nobody else has. So if anybody wants to add those reprs to the reference, feel free to send in a PR. It's a definitely glaring hole.</p>



<a name="207365804"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207365804" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207365804">(Aug 19 2020 at 03:04)</a>:</h4>
<p>Oh.</p>
<p>Well, yet another reason to never trust the rust reference. you know, that one document that's supposed be authoritative. better not trust that.</p>



<a name="207366195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207366195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207366195">(Aug 19 2020 at 03:10)</a>:</h4>
<p>I mean, it's not wrong, just incomplete.</p>



<a name="207366779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207366779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207366779">(Aug 19 2020 at 03:26)</a>:</h4>
<p>well it covers a few cases and then goes out of the way to say that any other combination is unspecified.</p>



<a name="207366783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207366783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207366783">(Aug 19 2020 at 03:26)</a>:</h4>
<p>So, since it is specified</p>



<a name="207373285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Interacting%20with%20non-repr%28C%29%20types%20in%20FFI/near/207373285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Interacting.20with.20non-repr(C).20types.20in.20FFI.html#207373285">(Aug 19 2020 at 06:04)</a>:</h4>
<p><span class="user-mention" data-user-id="296643">@isHavvy</span> is there an open issue for the missing doc ? If no, do you think opening one and linking to your previous try is possible ? If yes, could you ping me on it (@poliorcetics), I’ll have a look before the week is over <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>